package com.yk.template.spring.normal.dao.user.impl;

import com.yk.template.spring.normal.dao.common.api.ICommonDao;
import com.yk.template.spring.normal.dao.user.api.IUserDao;
import com.yk.template.spring.normal.model.user.UserModel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Project Name:spring-normal
 * File Name:UserDaoImpl
 * Package Name:com.yk.template.spring.normal.dao.user.impl
 * Date:2017/9/18 11:58
 * Author:zhangju
 * Description:
 * Copyright (c) 2017, 重庆云凯科技有限公司 All Rights Reserved.
 */


@Repository("userDao")
public class UserDaoImpl implements IUserDao {

    @Resource(name = "commonDao")
    private ICommonDao commonDao;

    protected final Log log= LogFactory.getLog(UserDaoImpl.class);

    /**
     * 查询所有用户实体对象
     *
     * @return 用户实体对象列表
     */
    @Override
    public List<UserModel> selectAll() {
        String hql="from UserModel as model";
        Session session=this.commonDao.getSession();
        Query<UserModel> query=session.createQuery(hql,UserModel.class);
        List<UserModel> models=query.list();
        session.close();
        return models;
    }

    /**
     * 根据条件 and 查询用户实体对象
     *
     * @param model 条件实体对象
     * @return 用户实体对象列表
     */
    @Override
    public List<UserModel> selectByAndCondition(UserModel model) {
        return null;
    }

    /**
     * 根据条件 or 查询用户实体对象
     *
     * @param model 条件实体对象
     * @return 用户实体对象列表
     */
    @Override
    public List<UserModel> selectByOrCondition(UserModel model) {
        return null;
    }

    /**
     * 根据条件 id 查询用户实体对象
     *
     * @param model 条件实体对象
     * @return 用户实体对象列表
     */
    @Override
    public UserModel select(UserModel model) {
        String hql="from UserModel as model where model.id=:id";
        Session session=this.commonDao.getSession();
        Query<UserModel> query=session.createQuery(hql,UserModel.class);
        query.setParameter("id",model.getId());
        UserModel result=new UserModel();
        try {
            result=query.getSingleResult();
        }catch (Exception e){
            this.log.error(" No entity found for query",e);
        }finally {
            session.close();
        }
        return result;
    }
}
